package com.tbit.main.controller;

import com.tbit.common.enums.OperaPswEnum;
import com.tbit.common.group.DefaultGroupA;
import com.tbit.main.constant.OperaPswConstant;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.pojo.Result;
import com.tbit.main.service.OperaPswService;
import com.tbit.manager.api.VipCardApi;
import com.tbit.manager.api.dto.PackageDelayDTO;
import com.tbit.manager.api.dto.UserMemberDTO;
import com.tbit.manager.api.dto.VipPageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 会员卡
 */
@RestController
@RequestMapping("/vipCard")
public class VipCardController {

    @Autowired
    private VipCardApi vipCardApi;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 用户购买会员卡记录
     */
    @GetMapping("/userVipPage")
    public Result<List<UserMemberDTO>> userVipPage(@Validated VipPageDTO vipPageDTO) {
        List<UserMemberDTO> list = vipCardApi.userVipPage(vipPageDTO.getAccountId(), vipPageDTO.getUserId(),
                vipPageDTO.getPhone(), vipPageDTO.getPageNumber(), vipPageDTO.getPageSize(), vipPageDTO.getStartTime(),
                vipPageDTO.getEndTime());
        return Result.success(list);
    }

    /**
     * 延长会员卡到期时间
     *
     * @param delayDTO .
     * @return .
     */
    @GetMapping("/modifyExpireTime")
    public Result<String> modifyVipCardExpireTime(@Validated(DefaultGroupA.class) PackageDelayDTO delayDTO) {
        /**操作密码校验 兼容5.0.0之前版本 常量校验不通过时，再调接口校验*/
        if (!OperaPswConstant.USER_OPERA_UPDATE_MONEY.equals(delayDTO.getPwd())) {
            operaPswService.checkOperaPsw(LoginUserContextHolder.getLoginUser(), OperaPswEnum.OPERA_PSW_COUPONS_CONFIG.getModule(),
                    delayDTO.getPwd());
        }
        String res = vipCardApi.modifyVipCardExpireTime(delayDTO);
        return Result.success(res);
    }
}
